﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_ShopDetail : System.Web.UI.Page
{
    protected string m_title = "";
    protected string m_detail = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();
        GssDO.DOShop DO = getById(id);
        if (DO != null && DO.IsDeleted == 0)
        {
            m_title = DO.Title;
            m_detail = ShowDetail(DO);
            lstProduct.DataSource = GetProductByShop(DO.PKIDShop, 5);
            lstProduct.DataBind();
        }
        else
            Response.Redirect(@"Shop.aspx", false);
    }

    private string ShowDetail(GssDO.DOShop DO)
    {
        string rs = "";

        rs = "<p>";
        if (DO.Image != "")
        {
            rs += "<table align='left'><tr><td style='padding: 0px 10px 2px 0px;'>";
            rs += "<img alt='" + DO.Title.Trim() + "' src='../Photos/Shop/" + DO.Image + "' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</td></tr></table>";
        }
        if (DO.Title.Trim() != "")
            rs += "<h3>" + DO.Title.Trim() + "</h3>";
        if (DO.Address.Trim() != "")
            rs += "<span class='name'>Địa chỉ:</span><span class='text'>" + DO.Address.Trim() + "</span><br>";
        if (DO.Phone.Trim() != "")
            rs += "<span class='name'>Điện thoại:</span><span class='text'>" + DO.Phone.Trim() + "</span><br>";
        if (DO.Email.Trim() != "")
            rs += "<span class='name'>Email:</span><span class='text'>" + DO.Email.Trim() + "</span><br>";
        if (DO.Website.Trim() != "")
            rs += "<span class='name'>Website:</span><span class='text'>" + DO.Website.Trim() + "</span><br>";
        GssDO.DOState sta = getStateById(DO.StateID);
        GssDO.DOLocation loc = getLocationById(DO.LocationID);
        if (sta != null && sta.IsDeleted == 0 && loc != null && loc.IsDeleted == 0)
        {
            rs += "<span class='name'>Khu vực:</span>";
            rs += "<a class='link' href='Shop.aspx?state=" + sta.PKIDState + "'>" + sta.Title.Trim() + "</a> &raquo; ";
            rs += "<a class='link' href='Shop.aspx?state=" + sta.PKIDState + "&location=" + loc.PKIDLocation + "'>" + loc.Title.Trim() + "</a>";
        }
        rs += "</p><p><br></p>";
        rs += "<p>" + DO.Description.Trim() + "</p>";
        
        return rs;
    }

    protected string BindProduct(string id, string title, string image, string shortText, object price, object realPrice, object productType)
    {
        string rs = "";
        double p = Convert.ToDouble(price), r = Convert.ToDouble(realPrice);
        int type = Convert.ToInt32(productType);
        // type=1: sales off; type=2: new arrival; type=3 hot product
        if (image != "")
        {
            rs += "<table align='center'><tr><td style='padding: 4px;'><a title='" + shortText + "' href='ProductDetail.aspx?id=" + id + "'>";
            rs += "<img alt='" + shortText + "' src='ProductThumbnail.aspx?id=" + id + "&width=110' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a title='" + shortText + "' href='ProductDetail.aspx?id=" + id + "'>" + title + "</a>";
        if (type == 1)
            rs += " <img alt='' border='0' class='icon' src='../images/sale.gif' /></p>";
        else if (type == 2)
            rs += " <img alt='' border='0' class='icon' src='../images/new.gif' /></p>";
        else if (type == 3)
            rs += " <img alt='' border='0' class='icon' src='../images/hot.gif' /></p>";
        else
            rs += "</p>";
        if (p != 0)
        {
            if (r != 0 && r < p)
            {
                rs += "<p class='price'><s>" + p.ToString("##,###") + " VNĐ</s><br><span>" + r.ToString("##,###") + " VNĐ</span></p>";
            }
            else
                rs += "<p class='price'><span>" + p.ToString("##,###") + " VNĐ</span></p>";
        }
        else if (p == 0)
            rs += "<p class='price'>Giá liên hệ</p>";
        return rs;
    }

    private DataTable GetProductByShop(string shopID, int row)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            dt = BL.getProductByShop(shopID, row);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    private GssDO.DOState getStateById(string id)
    {
        GssDO.DOState DO = null;
        try
        {
            GssBL.BLState BL = new GssBL.BLState();
            DO = BL.getStateByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOLocation getLocationById(string id)
    {
        GssDO.DOLocation DO = null;
        try
        {
            GssBL.BLLocation BL = new GssBL.BLLocation();
            DO = BL.getLocationByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOShop getById(string id)
    {
        GssDO.DOShop DO = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            DO = BL.getShopByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }
}
